package com.cskaoyan.shopping.api;


import com.cskaoyan.mall.commons.result.ServiceCallResponse;
import com.cskaoyan.mall.commons.result.ServiceCallResponseUtil;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.shopping.service.ICartOrderService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @auther cskaoyan
 */
@RestController
public class OrderApiImpl {

    @Autowired
    IProductService productService;

    @Autowired
    ICartOrderService iCartOrderService;


    @DeleteMapping(value = "/rpc/cart/items")
    ServiceCallResponse clearCartItemByUserID(@RequestBody ClearCartItemRequest request){

        ServiceCallResponseUtil<Object> serviceCallResponseUtil = new ServiceCallResponseUtil<>();
        try{
            iCartOrderService.clearCartItemByUserID(request);
        }catch (Exception e){
            return serviceCallResponseUtil.fail("清空购物车失败");
        }

        return serviceCallResponseUtil.ok(null);
    }

    @GetMapping(value = "/rpc/product/detail")
    ServiceCallResponse<ProductDetailDto> getProductDetail(@RequestParam("id") Long id) {
        ProductDetailDto productDetail = productService.getProductDetail(id);
        // 服务调用的返回值
        ServiceCallResponseUtil<ProductDetailDto> responseUtil = new ServiceCallResponseUtil<>();
        return responseUtil.ok(productDetail);
    }

}
